#region Using

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Reflection;

#endregion

namespace Bronze.ReportBuilder.BusinessComponent
{
    public partial class WizardStepUI : UserControl
    {
        public Action<BusinessAttribute> ItemChangeHandler;

        public EventHandler LoadItemCompleted;

        public int Index
        {
            get;
            set;
        }

        public void SetCurrentBusiness(Guid _selectedItem)
        {
            var list = this.listBoxBusiness.DataSource as List<BusinessAttribute>;
            foreach (var bus in list)
            {
                if (bus.Identify == _selectedItem)
                {
                    listBoxBusiness.SelectedValue = _selectedItem;
                    break;
                }
            }
        }

        public BusinessAttribute GetCurrentBusiness()
        {
            var item = this.listBoxBusiness.SelectedItem;
            if (item == null)
            {
                return null;
            }
            else
            {
                return item as BusinessAttribute;
            }
        }

        public Type RelationType
        {
            get;
            set;
        }

        public WizardStepUI()
        {
            InitializeComponent();
            
        }

        bool businessLoaded = false;

        

        public void LoadBusinessItem()
        {
            businessLoaded = false;
            List<BusinessAttribute> business = null;
            if (RelationType == null)
            {
                if (radioCombobox.Checked)
                {
                    business = BusinessAttribute.ComboBusiness.Where(o => o.CanBeTopLevel).ToList();
                }
                else
                {
                    business = BusinessAttribute.TreeComboBusiness.Where(o => o.CanBeTopLevel).ToList();
                }
            }
            else
            {
                var bn = BusinessAttribute.GetBusiness(RelationType);
                if (bn == null) return;
                if (radioCombobox.Checked)
                {
                    business = BusinessAttribute.ComboBusiness.Where(o => o.CanBeTopLevel == false).ToList();
                }
                else
                {
                    business = BusinessAttribute.TreeComboBusiness.Where(o => o.CanBeTopLevel == false).ToList();
                }
                var frm = this.Form as FrmCascadeWizard;
                if (frm == null) return;

                List<BusinessAttribute> newBusiness = new List<BusinessAttribute>();
                foreach (var item in business)
                {
                    if (frm.HasChoosedBusiness(item.OwnerType))
                    {
                        continue;
                    }
                    var exsitRelation = BusinessAttribute.ExsitRelation(item.OwnerType, RelationType);

                    if (exsitRelation)
                    {
                        newBusiness.Add(item);
                    }
                }
                business = newBusiness;
            }
            listBoxBusiness.DataSource = business;
            listBoxBusiness.SelectedIndex = -1;

            businessLoaded = true;
            if (LoadItemCompleted != null)
            {
                LoadItemCompleted(this, EventArgs.Empty);
            }
        }

        private void listBoxBusiness_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!businessLoaded) return;
            var selectedBusiness = listBoxBusiness.SelectedItem as BusinessAttribute;
            if (ItemChangeHandler != null)
            {
                ItemChangeHandler(selectedBusiness);
            }
            var frm = this.Form as FrmCascadeWizard;
            if (frm == null) return;
            frm.CreateOrRefreshWizard(this.Index + 1, selectedBusiness.OwnerType);
        }

        private void WizardStep_Load(object sender, EventArgs e)
        {
            if (this.DesignMode || this.Context == null)
            {
                return;
            }
            LoadBusinessItem();
        }

        public WizardStep Clone()
        {
            var step = new WizardStep();
            step.CurrentBusiness = this.GetCurrentBusiness();
            step.Index = this.Index;
            step.RelationType = RelationType;
            return step;
        }
    }


    [Serializable]
    public class WizardStepBase
    {
        public int Index
        {
            get;
            set;
        }

        public BusinessAttribute CurrentBusiness
        {
            get;
            set;
        }

        public Type RelationType
        {
            get;
            set;
        }

        

        public string ControlName;

      


    }

    [Serializable]
    public class WizardStep : WizardStepBase
    {
        public LabelFormControl Control;

        public WizardStepBase Copy()
        {
            var step = new WizardStepBase();
            step.ControlName = Control.Name;
            step.CurrentBusiness = this.CurrentBusiness;
            step.Index = this.Index;
            step.RelationType = RelationType;
            return step;
        }


    }
}